package com.danger.Activity.Account;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.danger.Activity.BackHandledFragment;
import com.danger.Activity.IndexActivity;
import com.danger.Activity.R;
import com.danger.Presenter.LoginPresenterImpl;
import com.danger.View.LoginView;

/**
 * Created by danger on 2016/4/24.
 */
public class LoginFragment extends BackHandledFragment implements View.OnClickListener, LoginView {

    private boolean hadIntercept;
    private View view;
    private EditText username_editText;
    private EditText password_editText;
    private Button login_button;
    private Button forget_password_button;
    private Button register_button;
    private ProgressDialog dialog;
    private LoginPresenterImpl loginPresenterImpl;
    private Toolbar toolbar;
    private AccountActivity activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_login, container, false);
        initView();
        initEvent();
        return view;
    }

    private void initView() {
        activity = (AccountActivity) getActivity();
        toolbar = activity.getToolbar();
        toolbar.setTitle("");
        activity.getToolbarTitle().setText("登录");
        activity.setSupportActionBar(toolbar);
        loginPresenterImpl = new LoginPresenterImpl(activity, this);
        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("验证登录信息...");
        dialog.setCanceledOnTouchOutside(false);
        username_editText = (EditText) view.findViewById(R.id.username_editText);
        password_editText = (EditText) view.findViewById(R.id.password_editText);
        login_button = (Button) view.findViewById(R.id.login_button);
        forget_password_button = (Button) view.findViewById(R.id.forget_password_button);
        register_button = (Button) view.findViewById(R.id.register_button);
    }

    private void initEvent() {
        login_button.setOnClickListener(this);
        register_button.setOnClickListener(this);
        forget_password_button.setOnClickListener(this);
    }


    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void showSuccess() {
        Toast.makeText(activity, "登录成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(activity, error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void toIndex() {
        Intent intent = new Intent(activity, IndexActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                loginPresenterImpl.login(username_editText.getText().toString(), password_editText.getText().toString());
                break;
            case R.id.register_button:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.account_container, new RegisterFragment()).commit();
                break;
            case R.id.forget_password_button:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.account_container, new ForgetPasswordFragment()).commit();
                break;
            default:
                break;
        }
    }
}
